package launcher;


import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.ExportException;
import java.util.ArrayList;

import core.api.ResoAPI;
import core.impl.Client;
import core.impl.Reso;
import core.protocol.ProtocolAD.Type;

/**
 * Lanceur d'application.
 */
public class Lanceur {

	public static final String NAMERESO = "Reso";
	private static final int NUM_CLIENT = 4;
	public static final int DEFAULT_PORT = 1706;
	private static final Type type = Type.DolevKlaweRodeh;

	/**
	 * Point d'entrée de l'éxécutable.
	 * 
	 * @param args
	 *            Les arguments de la ligne de commande.  UnicastRemoteObject.unexportObject(registry,true);
	 * @throws NotBoundException 
	 */
	public static final void main(String args[]){
		ArrayList<Client> clients = new ArrayList<Client>();
		ResoAPI reso;
		
		Registry registry = null;
		
		try {	
			registry = LocateRegistry.createRegistry(DEFAULT_PORT);
		} 
		catch (ExportException e) 
		{
			try {
				registry = LocateRegistry.getRegistry(DEFAULT_PORT);
			} catch (RemoteException e1) {
				e1.printStackTrace();
			}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		try {	
			reso = new Reso();
			registry.rebind(NAMERESO, reso);
			System.out.println("Server ready!");
		} 
		catch (Exception e) {
			e.printStackTrace();
		}

		for(int i = 0; i<NUM_CLIENT;i++){
			try {
				clients.add(new Client(type));
			} catch (RemoteException e) {
				e.printStackTrace();
			} catch (NotBoundException e) {
				e.printStackTrace();
			}	
		}
	}
}
